# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

_realname=Pillow
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-pillow" "${MINGW_PACKAGE_PREFIX}-python3-pillow")
pkgver=3.3.0
_py2basever=2.7
_py3basever=3.5m
pkgrel=1
arch=('any')
license=('custom')
url="https://github.com/python-pillow/Pillow/"
depends=("${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-lcms2"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-tk")
options=('staticlibs')
source=(${_realname}-${pkgver}.tar.gz::https://github.com/python-pillow/Pillow/archive/${pkgver}.tar.gz
        freeze-support.patch)
sha256sums=('ae2fbb500c81100f7e374545d008019e6f90918386f620625c3b3b98faf88414'
            '81719ede14f64371ad2ed155424666049c137e4eef6dd7d1fc4af64405015eee')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/freeze-support.patch
  cd ${srcdir}
  cp -r "${_realname}-${pkgver}" "python2-build-${CARCH}"
  cp -r "${_realname}-${pkgver}" "python3-build-${CARCH}"
}

package_python3-pillow() {
  pkgdesc="Python Imaging Library (PIL) fork Python3 version (mingw-w64)"
  depends+=("${MINGW_PACKAGE_PREFIX}-python3")
  provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
  replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
  optdepends=("${MINGW_PACKAGE_PREFIX}-tk: for the ImageTK module")

  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})
  
  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}/" --optimize=0

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/LICENSE"
  install -dm755 "${pkgdir}${MINGW_PREFIX}/include/python${_py3basever}/"
  install -m644 -t "${pkgdir}${MINGW_PREFIX}/include/python${_py3basever}/" libImaging/*.h

  # clean up bins
  cd "${pkgdir}${MINGW_PREFIX}/bin"
  for f in *.py; do
  	sed -e "s|${_mingw_prefix}/bin/|/usr/bin/env |g" -i ${f}
    mv "$f" "${f%.py}"
  done
}

package_python2-pillow() {
  pkgdesc="Python Imaging Library (PIL) fork. Python2 version (mingw-w64)"
  depends+=("${MINGW_PACKAGE_PREFIX}-python2")
  provides=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}")
  replaces=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}")
  optdepends=("${MINGW_PACKAGE_PREFIX}-tk: for the ImageTK module")
  
  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})

  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX#\/} --root="${pkgdir}/" --optimize=0

  sed -i 's|/usr/local/bin/python$|/usr/bin/env python2|' \
    "${pkgdir}${MINGW_PREFIX}/lib/python${_py2basever}/site-packages/PIL/OleFileIO.py"

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE"

  install -dm755 "${pkgdir}${MINGW_PREFIX}/include/python${_py2basever}/"
  install -m644 -t "${pkgdir}${MINGW_PREFIX}/include/python${_py2basever}/" libImaging/*.h

  # clean up bins
  cd "${pkgdir}${MINGW_PREFIX}/bin"
  for f in *.py; do
	  sed -e "s|${_mingw_prefix}/bin/|/usr/bin/env |g" -i ${f}
    mv "${f}" "${f%.py}2"
  done
}

package_mingw-w64-i686-python2-pillow() {
  package_python2-pillow
}

package_mingw-w64-i686-python3-pillow() {
  package_python3-pillow
}

package_mingw-w64-x86_64-python2-pillow() {
  package_python2-pillow
}

package_mingw-w64-x86_64-python3-pillow() {
  package_python3-pillow
}
